package bb.lanxing.model.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.LitePal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import bb.lanxing.model.WorkoutDatabaseHelper;
import bb.lanxing.model.json.ServerUser;
import bb.lanxing.model.json.TrackSegment;
import bb.lanxing.model.json.WorkoutExtraInfo;
import bb.lanxing.util.Enums;
import bb.lanxing.util.JsonUtil;
import bb.lanxing.util.Log;
import gov.nist.core.Separators;

public class Workout extends LitePalTableBase implements Serializable, Parcelable, IWorkout, Cloneable {
    public static final int CADENCE_SOURCE_OTHER = -1;
    public static final int CADENCE_SOURCE_NONE = 0;
    public static final int CADENCE_SOURCE_BICI = 1;
    public static final int CADENCE_SOURCE_BLE = 2;
    public static final int CADENCE_SOURCE_IGPS = 3;
    public static final int CADENCE_SOURCE_QI = 4;
    public static final int CADENCE_SOURCE_BRYTON = 5;
    public static final int CADENCE_SOURCE_MAGENE = 6;
    public static final int CADENCE_SOURCE_GARMIN = 7;
    public static final int CADENCE_SOURCE_THINKRIDER = 8;
    public static final int CADENCE_SOURCE_SPRINT = 9;
    public static final Creator<Workout> CREATOR = new Creator<Workout>() {
        @Override
        public Workout createFromParcel(Parcel source) {
            return new Workout(source);
        }

        @Override
        public Workout[] newArray(int size) {
            return new Workout[size];
        }
    };
    public static final int HEARTRATE_SOURCE_OTHER = -1;
    public static final int HEARTRATE_SOURCE_NONE = 0;
    public static final int HEARTRATE_SOURCE_BICI = 1;
    public static final int HEARTRATE_SOURCE_BLE = 2;
    public static final int HEARTRATE_SOURCE_IGPS = 3;
    public static final int HEARTRATE_SOURCE_BRYTON = 4;
    public static final int HEARTRATE_SOURCE_GARMIN = 5;
    public static final int HEARTRATE_SOURCE_THINKRIDER = 6;
    public static final int HEARTRATE_SOURCE_SPRINT = 7;
    public static final int HEARTRATE_SOURCE_XOSS_X2P = 8;
    public static final int LEGAL_BREAK_TIME_INTERVAL = 3600000;
    public static final int LOCATION_SOURCE_OTHER = -1;
    public static final int LOCATION_SOURCE_NONE = 0;
    public static final int LOCATION_SOURCE_PHONE = 1;
    public static final int LOCATION_SOURCE_BICI = 2;
    public static final int LOCATION_SOURCE_IGPS = 3;
    public static final int LOCATION_SOURCE_BRYTON = 4;
    public static final int LOCATION_SOURCE_MAGENE = 5;
    public static final int LOCATION_SOURCE_GARMIN = 6;
    public static final int LOCATION_SOURCE_THINKRIDER = 7;
    public static final int LOCATION_SOURCE_TIC_WATCH = 8;
    public static final int LOCATION_SOURCE_ANDROID_WEAR = 9;
    public static final int LOCATION_SOURCE_SPRINT = 10;
    public static final int LOCATION_SOURCE_DISCOVERY = 11;
    public static final int LOCATION_SOURCE_XPLOVA = 12;
    public static final int LOCATION_SOURCE_SUUNTO = 13;
    public static final int LOCATION_SOURCE_XOSS_G = 14;
    public static final int LOCATION_SOURCE_XOSS_NAV = 20;
    public static final int LOCATION_SOURCE_BES_WATCH = 30;
    public static final int LOCATION_SOURCE_XOSS_NAV_PLUS = 21;
    public static final int LOCATION_SOURCE_CYCPLUS_M1 = 40;
    public static final int LOCATION_SOURCE_CYCPLUS_M2 = 41;
    public static final int LOCATION_SOURCE_CHILEAF_600 = 50;
    public static final int LOCATION_SOURCE_XOSS_X2P = 100;
    public static final int POWER_SOURCE_NONE = 0;
    public static final int POWER_SOURCE_QI = 1;
    public static final int POWER_SOURCE_EST = 2;
    public static final int POWER_SOURCE_OTHER = 100;
    public static final int INDOORBIKE_SOURCE_OTHER = 1;
    public static final int CROSSTRAINER_SOURCE_OTHER = 1;
    public static final int ROWER_SOURCE_OTHER = 1;
    public static final int STATUS_UNSTART = 0;
    public static final int STATUS_GPS_SEARCHING = 16;
    public static final int STATUS_WORKING = 17;
    public static final int STATUS_PAUSED = 18;
    public static final int STATUS_STOPED = 32;
    public static final int TYPE_OTHER = 0;
    public static final int TYPE_WALKING = 1;
    public static final int TYPE_RUNNING = 2;
    public static final int TYPE_CYCLING = 3;
    public static final int TYPE_HIKING = 4;
    public static final int TYPE_SWIMMING = 5;
    public static final int TYPE_SKIING = 6;
    public static final int TYPE_TRAINING = 8;
    public static final int TYPE_INDOOR_BIKE = 11;
    public static final int TYPE_VIRTUAL_ACTIVITY = 12;
    public static final int TYPE_ROWER = 13;
    public static final int TYPE_CROSS_TRAINER = 14;
    public static final int TYPE_BIKE_TRAINER = 15;
    private static final String TAG = "Workout=======";
    private static final String COLUMNS_FOR_HISTORY_LIST = "id, sport, startTime, distance, duration, uploadStatus, uuid, serverId, title, synchronise, locSource, calorie, elevationGain, hidden, cadenceSource, heartSource, isValid, credits, matchedSegments, threedWorkout, likeCount, commentCount, isExport";
    private static final long serialVersionUID = 1;
    private static SQLiteDatabase db = null;
    public int feeling;
    private int avgCadence;
    private int avgHeartrate;
    private double avgHeatKm;
    private String avgHeatKmRate;
    private double avgPower;
    private double avgSpeed;
    private int cadence;
    private int cadenceSource;
    private int calorie;
    private int category;
    private int commentCount;
    private double credits;
    private double creditsDvalue;
    private String description;
    private double distance;
    private double downDistance;
    private long downDuration;
    private long duration;
    private float elevationGain;
    private float elevationLoss;
    private String encodingPoints;
    private int endCadence;
    private long endTime;
    private int endWheel;
    private String equipmentInfo;
    private String fitPath;
    private double flatDistance;
    private long flatDuration;
    private boolean fullData;
    private int fullDataValue;
    private float grade;
    private int heartSource;
    private int heartrate;
    private int hidden;
    private boolean isExport;
    private boolean isLike;
    private boolean isValid;
    private long lastUpload;
    private int likeCount;
    private int locSource;
    private int mapHidden;
    private int mapId;
    private String mapUrl;
    private int matchedSegments;
    private int maxCadence;
    private int maxGrade;
    private int maxHeartrate;
    private double maxPower;
    private double maxSpeed;
    private int maxWheelRevolution;
    private String mergeRecord;
    private int minGrade;
    private long modifyTime;
    private long pauseDuration;
    private String poi;
    private long pointCounts;
    private double powerFTP;
    private double powerIF;
    private double powerNP;
    private int powerSource;
    private double powerTSS;
    private double powerVI;
    private double realtimeSpeed;
    private String segmentCa;
    private String segmentHr;
    private String segmentIndex;
    private String segmentKM;
    private String segmentSport;
    private long serverId;
    private ServerUser serverUser;
    private String slopeGradeMax;
    private int sport;
    private int startCadence;
    private long startTime;
    private int startWheel;
    private int step;
    private boolean synchronise;
    private String threedWorkout;
    private String thumbnail;
    private String title;
    private List<Trackpoint> trackPoints;
    private List<PacePoint> pacePoints;
    private List<TrackSegment> trackSegments;
    private int typeChangeCount;
    private double upDistance;
    private long upDuration;
    private int uploadStatus;
    private long uploadTime;
    private long userId;
    private String uuid;
    private int workStatus;

    public Workout() {
        locSource = LOCATION_SOURCE_PHONE;
        synchronise = false;
        fullData = true;
        category = 0;
        mapHidden = 0;
    }

    public Workout(JSONObject node, boolean isMine) throws JSONException {
        Log.d(TAG, "Workout...node=" + node);
        locSource = LOCATION_SOURCE_PHONE;

        synchronise = false;
        fullData = true;
        category = 0;
        mapHidden = 0;
        if (isMine) {
            setId(JsonUtil.getLongValue("clientId", node));
            setServerId(JsonUtil.getLongValue("serverId", node));
            setUserId(JsonUtil.getLongValue("userId", node));//z.m
            setSegmentKM(JsonUtil.getStringValue("segmentKM", node));
        } else {//todo sst need check
            setServerId(JsonUtil.getLongValue("id", node));
            setUserId(JsonUtil.getLongValue("user_id", node));
            JSONObject objectValue = JsonUtil.getObjectValue("user", node);//z.m
            if (objectValue != null) {
                setServerUser(new ServerUser(objectValue));
            }
        }
        setSport(JsonUtil.getIntegerValue("sport", node));
        setStartTime(JsonUtil.getLongValue("startTime", node));//sst d.p
        setEndTime(JsonUtil.getLongValue("endTime", node));//sst d.q
        setDistance(JsonUtil.getDoubleValue("distance", node));
        setDuration(JsonUtil.getLongValue("duration", node));
        setUploadStatus(Enums.UploadStatus.Uploaded);
        setTitle(JsonUtil.getStringValue("title", node));
        setMinGrade(JsonUtil.getIntegerValue("minGrade", node));
        setMaxGrade(JsonUtil.getIntegerValue("maxGrade", node));
        setElevationGain(JsonUtil.getIntegerValue("elevationGain", node));
        setElevationLoss(JsonUtil.getIntegerValue("elevationLoss", node));
        setUpDistance(JsonUtil.getDoubleValue("upDistance", node));
        setUpDuration(JsonUtil.getLongValue("upDuration", node));
        setDownDistance(JsonUtil.getDoubleValue("downDistance", node));
        setDownDuration(JsonUtil.getLongValue("downDuration", node));
        setFlatDistance(JsonUtil.getDoubleValue("flatDistance", node));
        setFlatDuration(JsonUtil.getLongValue("flatDuration", node));
        setUuid(JsonUtil.getStringValue("uuid", node));
        setHidden(JsonUtil.getIntegerValue("hidden", node));
        setAvgSpeed(JsonUtil.getDoubleValue("avgSpeed", node));
        setMaxSpeed(JsonUtil.getDoubleValue("maxSpeed", node));
        setDescription(JsonUtil.getStringValue("description", node));
        setMaxCadence(JsonUtil.getIntegerValue("maxCadence", node));
        setAvgCadence(JsonUtil.getIntegerValue("avgCadence", node));
        setStartCadence(JsonUtil.getIntegerValue("startCadence", node));
        setEndCadence(JsonUtil.getIntegerValue("endCadence", node));
        setMaxHeartrate(JsonUtil.getIntegerValue("maxHeartrate", node));
        setAvgHeartrate(JsonUtil.getIntegerValue("avgHeartrate", node));
        setStartWheel(JsonUtil.getIntegerValue("startSheel", node));
        setEndWheel(JsonUtil.getIntegerValue("endWheel", node));
        setMaxWheelRevolution(JsonUtil.getIntegerValue("maxWheelRevolution", node));
        setLocSource(JsonUtil.getIntegerValue("locSource", node));
        setHeartSource(JsonUtil.getIntegerValue("heartSource", node));
        setCadenceSource(JsonUtil.getIntegerValue("cadenceSource", node));
        setEncodingPoints(JsonUtil.getStringValue("encodingPoints", node));
        setSegmentIndex(JsonUtil.getStringValue("segmentIndex", node));
        setSegmentSport(JsonUtil.getStringValue("segmentSport", node));
        setSegmentHr(JsonUtil.getStringValue("segmentHr", node));
        setSegmentCa(JsonUtil.getStringValue("segmentCa", node));
        setCalorie(JsonUtil.getIntegerValue("calorie", node));
        setCredits(JsonUtil.getDoubleValue("credits", node));
        setCreditsDvalue(JsonUtil.getDoubleValue("creditsDvalue", node));
        setAvgHeatKmRate(JsonUtil.getStringValue("avgHeatKmRate", node));
        setAvgHeatKm(JsonUtil.getDoubleValue("avgHeatKm", node));
        setStep(JsonUtil.getIntegerValue("step", node));
        setIsValid(JsonUtil.getIntegerValue("isValid", node) != 0);
        setPoi(JsonUtil.getStringValue("poi", node));
        setMatchedSegments(JsonUtil.getIntegerValue("isSegment", node));
        setIsLike(JsonUtil.getIntegerValue("isLike", node) != 0);
        setLikeCount(JsonUtil.getIntegerValue("likeCount", node));
        setCommentCount(JsonUtil.getIntegerValue("commentCount", node));
        setThreedWorkout(JsonUtil.getStringValue("threedWorkout", node));
        setIsExport(JsonUtil.getIntegerValue("isExport", node) != 0);
        setThumbnail(JsonUtil.getStringValue("thumbnail", node));
        setModifyTime(JsonUtil.getLongValue("modifyTime", node));
        String stringValue = JsonUtil.getStringValue("segments", node);//todo sst ?
        if (!TextUtils.isEmpty(stringValue)) {
            List<TrackSegment> parseArray = JSON.parseArray(stringValue, TrackSegment.class);
            trackSegments = parseArray;
            if (parseArray != null && !parseArray.isEmpty()) {
                setMatchedSegments(1);
            }
        }
        setPowerSource(JsonUtil.getIntegerValue("powerSource", node));
        setMaxPower(JsonUtil.getDoubleValue("powerMax", node));
        setAvgPower(JsonUtil.getDoubleValue("powerAvg", node));
        setPowerNP(JsonUtil.getDoubleValue("powerNP", node));
        setPowerIF(JsonUtil.getDoubleValue("powerIF", node));
        setPowerVI(JsonUtil.getDoubleValue("powerVI", node));
        setPowerTSS(JsonUtil.getDoubleValue("powerTSS", node));
        if (node.has("powerFTP")) {
            setPowerFTP(JsonUtil.getDoubleValue("powerFTP", node));
        }
        setTypeChangeCount(JsonUtil.getIntegerValue("sportChangeCount", node));//todo: sst
        setCategory(JsonUtil.getIntegerValue("category", node));
        setMergeRecord(JsonUtil.getStringValue("mergeRecord", node));
        setMapHidden(JsonUtil.getIntegerValue("mapHidden", node));
        setMapId(JsonUtil.getIntegerValue("mapId", node));
        setSlopeGradeMax(JsonUtil.getStringValue("slopeGradeMax", node));
    }

    protected Workout(Parcel parcel) {
        boolean z = true;
        locSource = 1;
        synchronise = false;
        fullData = true;
        category = 0;
        mapHidden = 0;
        id = parcel.readLong();
        if (id < 0) {
            id = null;
        }
        sport = parcel.readInt();
        startTime = parcel.readLong();
        endTime = parcel.readLong();
        distance = parcel.readDouble();
        duration = parcel.readLong();
        userId = parcel.readLong();
        uploadStatus = parcel.readInt();
        lastUpload = parcel.readLong();
        serverId = parcel.readLong();
        title = parcel.readString();
        minGrade = parcel.readInt();
        maxGrade = parcel.readInt();
        elevationGain = parcel.readFloat();
        elevationLoss = parcel.readFloat();
        upDistance = parcel.readDouble();
        upDuration = parcel.readLong();
        downDistance = parcel.readDouble();
        downDuration = parcel.readLong();
        flatDistance = parcel.readDouble();
        flatDuration = parcel.readLong();
        uuid = parcel.readString();
        hidden = parcel.readInt();
        avgSpeed = parcel.readDouble();
        maxSpeed = parcel.readDouble();
        realtimeSpeed = parcel.readDouble();
        pauseDuration = parcel.readLong();
        avgCadence = parcel.readInt();
        maxCadence = parcel.readInt();
        avgHeartrate = parcel.readInt();
        maxHeartrate = parcel.readInt();
        startCadence = parcel.readInt();
        startWheel = parcel.readInt();
        endCadence = parcel.readInt();
        endWheel = parcel.readInt();
        maxWheelRevolution = parcel.readInt();
        locSource = parcel.readInt();
        heartSource = parcel.readInt();
        cadenceSource = parcel.readInt();
        encodingPoints = parcel.readString();
        pointCounts = parcel.readLong();
        matchedSegments = parcel.readInt();
        segmentIndex = parcel.readString();
        segmentSport = parcel.readString();
        segmentHr = parcel.readString();
        segmentCa = parcel.readString();
        cadence = parcel.readInt();
        heartrate = parcel.readInt();
        calorie = parcel.readInt();
        step = parcel.readInt();
        credits = parcel.readDouble();
        creditsDvalue = parcel.readDouble();
        avgHeatKm = parcel.readDouble();
        avgHeatKmRate = parcel.readString();
        likeCount = parcel.readInt();
        commentCount = parcel.readInt();
        isLike = parcel.readByte() != 0;
        threedWorkout = parcel.readString();
        isExport = parcel.readByte() != 0;
        isValid = parcel.readByte() != 0;
        poi = parcel.readString();
        synchronise = parcel.readByte() != 0;
        serverUser = (ServerUser) parcel.readParcelable(ServerUser.class.getClassLoader());
        mapUrl = parcel.readString();
        thumbnail = parcel.readString();
        description = parcel.readString();
        fullData = parcel.readByte() == 0 ? false : z;
        uploadTime = parcel.readLong();
        modifyTime = parcel.readLong();
        workStatus = parcel.readInt();
        powerSource = parcel.readInt();
        maxPower = parcel.readDouble();
        avgPower = parcel.readDouble();
        powerNP = parcel.readDouble();
        powerIF = parcel.readDouble();
        powerVI = parcel.readDouble();
        powerTSS = parcel.readDouble();
        powerFTP = parcel.readDouble();
        typeChangeCount = parcel.readInt();
        category = parcel.readInt();
        mergeRecord = parcel.readString();
        fitPath = parcel.readString();
        equipmentInfo = parcel.readString();
        grade = parcel.readFloat();
        mapHidden = parcel.readInt();
        mapId = parcel.readInt();
        slopeGradeMax = parcel.readString();
    }

    public static long getSerialVersionUID() {
        return 1L;
    }

    public static Workout parseCursor(Cursor c) {
        return parseCursor(c, null);
    }

    public static Workout parseCursor(Cursor cursor, Map<String, Integer> map) {
        Workout workout = new Workout();
        int index = getIndex(cursor, map, "ID");
        if (index >= 0) {
            workout.id = cursor.getLong(index);
        }
        int index2 = getIndex(cursor, map, "SPORT");
        if (index2 >= 0) {
            workout.sport = cursor.getInt(index2);
        }
        int index3 = getIndex(cursor, map, "STARTTIME");
        if (index3 >= 0) {
            workout.startTime = cursor.getLong(index3);
        }
        int index4 = getIndex(cursor, map, "ENDTIME");
        if (index4 >= 0) {
            workout.endTime = cursor.getLong(index4);
        }
        int index5 = getIndex(cursor, map, "DISTANCE");
        if (index5 >= 0) {
            workout.distance = cursor.getDouble(index5);
        }
        int index6 = getIndex(cursor, map, "DURATION");
        if (index6 >= 0) {
            workout.duration = cursor.getLong(index6);
        }
        int index7 = getIndex(cursor, map, "USERID");
        if (index7 >= 0) {
            workout.userId = cursor.getLong(index7);
        }
        int index8 = getIndex(cursor, map, "UPLOADSTATUS");
        if (index8 >= 0) {
            workout.uploadStatus = cursor.getInt(index8);
        }
        int index9 = getIndex(cursor, map, "LASTUPLOAD");
        if (index9 >= 0) {
            workout.lastUpload = cursor.getLong(index9);
        }
        int index10 = getIndex(cursor, map, "SERVERID");
        if (index10 >= 0) {
            workout.serverId = cursor.getLong(index10);
        }
        int index11 = getIndex(cursor, map, "TITLE");
        if (index11 >= 0) {
            workout.title = cursor.getString(index11);
        }
        int index12 = getIndex(cursor, map, "MINGRADE");
        if (index12 >= 0) {
            workout.minGrade = cursor.getInt(index12);
        }
        int index13 = getIndex(cursor, map, "MAXGRADE");
        if (index13 >= 0) {
            workout.maxGrade = cursor.getInt(index13);
        }
        int index14 = getIndex(cursor, map, "ELEVATIONGAIN");
        if (index14 >= 0) {
            workout.elevationGain = cursor.getFloat(index14);
        }
        int index15 = getIndex(cursor, map, "ELEVATIONLOSS");
        if (index15 >= 0) {
            workout.elevationLoss = cursor.getFloat(index15);
        }
        int index16 = getIndex(cursor, map, "UPDISTANCE");
        if (index16 >= 0) {
            workout.upDistance = cursor.getDouble(index16);
        }
        int index17 = getIndex(cursor, map, "UPDURATION");
        if (index17 >= 0) {
            workout.upDuration = cursor.getLong(index17);
        }
        int index18 = getIndex(cursor, map, "DOWNDISTANCE");
        if (index18 >= 0) {
            workout.downDistance = cursor.getDouble(index18);
        }
        int index19 = getIndex(cursor, map, "DOWNDURATION");
        if (index19 >= 0) {
            workout.downDuration = cursor.getLong(index19);
        }
        int index20 = getIndex(cursor, map, "FLATDISTANCE");
        if (index20 >= 0) {
            workout.flatDistance = cursor.getDouble(index20);
        }
        int index21 = getIndex(cursor, map, "FLATDURATION");
        if (index21 >= 0) {
            workout.flatDuration = cursor.getLong(index21);
        }
        int index22 = getIndex(cursor, map, "UUID");
        if (index22 >= 0) {
            workout.uuid = cursor.getString(index22);
        }
        int index23 = getIndex(cursor, map, "HIDDEN");
        if (index23 >= 0) {
            workout.hidden = cursor.getInt(index23);
        }
        int index24 = getIndex(cursor, map, "AVGSPEED");
        if (index24 >= 0) {
            workout.avgSpeed = cursor.getDouble(index24);
        }
        int index25 = getIndex(cursor, map, "MAXSPEED");
        if (index25 >= 0) {
            workout.maxSpeed = cursor.getDouble(index25);
        }
        int index26 = getIndex(cursor, map, "REALTIMESPEED");
        if (index26 >= 0) {
            workout.realtimeSpeed = cursor.getDouble(index26);
        }
        int index27 = getIndex(cursor, map, "PAUSEDURATION");
        if (index27 >= 0) {
            workout.pauseDuration = cursor.getLong(index27);
        }
        int index28 = getIndex(cursor, map, "AVGCADENCE");
        if (index28 >= 0) {
            workout.avgCadence = cursor.getInt(index28);
        }
        int index29 = getIndex(cursor, map, "MAXCADENCE");
        if (index29 >= 0) {
            workout.maxCadence = cursor.getInt(index29);
        }
        int index30 = getIndex(cursor, map, "AVGHEARTRATE");
        if (index30 >= 0) {
            workout.avgHeartrate = cursor.getInt(index30);
        }
        int index31 = getIndex(cursor, map, "MAXHEARTRATE");
        if (index31 >= 0) {
            workout.maxHeartrate = cursor.getInt(index31);
        }
        int index32 = getIndex(cursor, map, "STARTCADENCE");
        if (index32 >= 0) {
            workout.startCadence = cursor.getInt(index32);
        }
        int index33 = getIndex(cursor, map, "STARTWHEEL");
        if (index33 >= 0) {
            workout.startWheel = cursor.getInt(index33);
        }
        int index34 = getIndex(cursor, map, "ENDCADENCE");
        if (index34 >= 0) {
            workout.endCadence = cursor.getInt(index34);
        }
        int index35 = getIndex(cursor, map, "ENDWHEEL");
        if (index35 >= 0) {
            workout.endWheel = cursor.getInt(index35);
        }
        int index36 = getIndex(cursor, map, "MAXWHEELREVOLUTION");
        if (index36 >= 0) {
            workout.maxWheelRevolution = cursor.getInt(index36);
        }
        int index37 = getIndex(cursor, map, "LOCSOURCE");
        if (index37 >= 0) {
            workout.locSource = cursor.getInt(index37);
        }
        int index38 = getIndex(cursor, map, "HEARTSOURCE");
        if (index38 >= 0) {
            workout.heartSource = cursor.getInt(index38);
        }
        int index39 = getIndex(cursor, map, "CADENCESOURCE");
        if (index39 >= 0) {
            workout.cadenceSource = cursor.getInt(index39);
        }
        int index40 = getIndex(cursor, map, "ENCODINGPOINTS");
        if (index40 >= 0) {
            workout.encodingPoints = cursor.getString(index40);
        }
        int index41 = getIndex(cursor, map, "POINTCOUNTS");
        if (index41 >= 0) {
            workout.pointCounts = cursor.getLong(index41);
        }
        int index42 = getIndex(cursor, map, "MATCHEDSEGMENTS");
        if (index42 >= 0) {
            workout.matchedSegments = cursor.getInt(index42);
        }
        int index43 = getIndex(cursor, map, "SEGMENTINDEX");
        if (index43 >= 0) {
            workout.segmentIndex = cursor.getString(index43);
        }
        int index44 = getIndex(cursor, map, "SEGMENTKM");
        if (index44 >= 0) {
            workout.segmentKM = cursor.getString(index44);
        }
        int index45 = getIndex(cursor, map, "SEGMENTSPORT");
        if (index45 >= 0) {
            workout.segmentSport = cursor.getString(index45);
        }
        int index46 = getIndex(cursor, map, "SEGMENTHR");
        if (index46 >= 0) {
            workout.segmentHr = cursor.getString(index46);
        }
        int index47 = getIndex(cursor, map, "SEGMENTCA");
        if (index47 >= 0) {
            workout.segmentCa = cursor.getString(index47);
        }
        int index48 = getIndex(cursor, map, "CADENCE");
        if (index48 >= 0) {
            workout.cadence = cursor.getInt(index48);
        }
        int index49 = getIndex(cursor, map, "HEARTRATE");
        if (index49 >= 0) {
            workout.heartrate = cursor.getInt(index49);
        }
        int index50 = getIndex(cursor, map, "CALORIE");
        if (index50 >= 0) {
            workout.calorie = cursor.getInt(index50);
        }
        int index51 = getIndex(cursor, map, "STEP");
        if (index51 >= 0) {
            workout.step = cursor.getInt(index51);
        }
        int index52 = getIndex(cursor, map, "CREDITS");
        if (index52 >= 0) {
            workout.credits = cursor.getDouble(index52);
        }
        int index53 = getIndex(cursor, map, "CREDITSDVALUE");
        if (index53 >= 0) {
            workout.creditsDvalue = cursor.getDouble(index53);
        }
        int index54 = getIndex(cursor, map, "AVGHEATKMRATE");
        if (index54 >= 0) {
            workout.avgHeatKmRate = cursor.getString(index54);
        }
        int index55 = getIndex(cursor, map, "AVGHEATKM");
        if (index55 >= 0) {
            workout.avgHeatKm = cursor.getDouble(index55);
        }
        int index56 = getIndex(cursor, map, "LIKECOUNT");
        if (index56 >= 0) {
            workout.likeCount = cursor.getInt(index56);
        }
        int index57 = getIndex(cursor, map, "COMMENTCOUNT");
        if (index57 >= 0) {
            workout.commentCount = cursor.getInt(index57);
        }
        int index58 = getIndex(cursor, map, "ISLIKE");
        boolean z = true;
        if (index58 >= 0) {
            workout.isLike = cursor.getInt(index58) != 0;
        }
        int index59 = getIndex(cursor, map, "THREEDWORKOUT");
        if (index59 >= 0) {
            workout.threedWorkout = cursor.getString(index59);
        }
        int index60 = getIndex(cursor, map, "ISEXPORT");
        if (index60 >= 0) {
            workout.isExport = cursor.getInt(index60) != 0;
        }
        int index61 = getIndex(cursor, map, "ISVALID");
        if (index61 >= 0) {
            workout.isValid = cursor.getInt(index61) != 0;
        }
        int index62 = getIndex(cursor, map, "POI");
        if (index62 >= 0) {
            workout.poi = cursor.getString(index62);
        }
        int index63 = getIndex(cursor, map, "SYNCHRONISE");
        if (index63 >= 0) {
            workout.synchronise = cursor.getInt(index63) != 0;
        }
        int index64 = getIndex(cursor, map, "MAPURL");
        if (index64 >= 0) {
            workout.mapUrl = cursor.getString(index64);
        }
        int index65 = getIndex(cursor, map, "THUMBNAIL");
        if (index65 >= 0) {
            workout.thumbnail = cursor.getString(index65);
        }
        int index66 = getIndex(cursor, map, "DESCRIPTION");
        if (index66 >= 0) {
            workout.description = cursor.getString(index66);
        }
        int index67 = getIndex(cursor, map, "FULLDATA");
        if (index67 >= 0) {
            workout.fullData = cursor.getInt(index67) != 0;
        }
        int index68 = getIndex(cursor, map, "UPLOADTIME");
        if (index68 >= 0) {
            workout.uploadTime = cursor.getLong(index68);
        }
        int index69 = getIndex(cursor, map, "MODIFYTIME");
        if (index69 >= 0) {
            workout.modifyTime = cursor.getLong(index69);
        }
        int index70 = getIndex(cursor, map, "WORKSTATUS");
        if (index70 >= 0) {
            workout.workStatus = cursor.getInt(index70);
        }
        int index71 = getIndex(cursor, map, "POWERSOURCE");
        if (index71 >= 0) {
            workout.powerSource = cursor.getInt(index71);
        }
        int index72 = getIndex(cursor, map, "MAXPOWER");
        if (index72 >= 0) {
            workout.maxPower = cursor.getDouble(index72);
        }
        int index73 = getIndex(cursor, map, "AVGPOWER");
        if (index73 >= 0) {
            workout.avgPower = cursor.getDouble(index73);
        }
        int index74 = getIndex(cursor, map, "POWERNP");
        if (index74 >= 0) {
            workout.powerNP = cursor.getDouble(index74);
        }
        int index75 = getIndex(cursor, map, "POWERIF");
        if (index75 >= 0) {
            workout.powerIF = cursor.getDouble(index75);
        }
        int index76 = getIndex(cursor, map, "POWERVI");
        if (index76 >= 0) {
            workout.powerVI = cursor.getDouble(index76);
        }
        int index77 = getIndex(cursor, map, "POWERTTS");
        if (index77 >= 0) {
            workout.powerTSS = cursor.getDouble(index77);
        }
        int index78 = getIndex(cursor, map, "POWERFTP");
        if (index78 >= 0) {
            workout.powerFTP = cursor.getDouble(index78);
        }
        int index79 = getIndex(cursor, map, "TYPECHANGECOUNT");
        if (index79 >= 0) {
            workout.typeChangeCount = cursor.getInt(index79);
        }
        int index80 = getIndex(cursor, map, "CATEGORY");
        if (index80 >= 0) {
            workout.category = cursor.getInt(index80);
        }
        int index81 = getIndex(cursor, map, "MERGERECORD");
        if (index81 >= 0) {
            workout.mergeRecord = cursor.getString(index81);
        }
        int index82 = getIndex(cursor, map, "FITPATH");
        if (index82 >= 0) {
            workout.fitPath = cursor.getString(index82);
        }
        int index83 = getIndex(cursor, map, "EQUIPMENTINFO");
        if (index83 >= 0) {
            workout.equipmentInfo = cursor.getString(index83);
        }
        int index84 = getIndex(cursor, map, "SLOPEGRADEMAX");
        if (index84 >= 0) {
            workout.slopeGradeMax = cursor.getString(index84);
        }
        return workout;
    }

    private static int getIndex(Cursor c, Map<String, Integer> indexCache, String colName) {
        if (indexCache == null) {
            int columnIndex = c.getColumnIndex(colName);
            if (columnIndex < 0) {
                columnIndex = c.getColumnIndex(colName.toLowerCase());
            }
            return columnIndex < 0 ? c.getColumnIndex(colName.toUpperCase()) : columnIndex;
        }
        Integer num = indexCache.get(colName);
        if (num != null) {
            return num;
        }
        return -1;
    }

    public static void saveSegmentAndPoints(Workout workout) throws JSONException {
        if (workout == null) {
            return;
        }
        Segment.createSegment(workout);
//        List<Segment> byWorkoutId = Segment.getByWorkoutId(workout.getId());
//        JSONArray jSONArray = new JSONArray();
//        for (Segment segment : byWorkoutId) {
//            jSONArray.put(Segment.toJson(segment));
//        }
//        workout.setSegmentIndex(jSONArray.toString());
//        workout.setPointCounts(Trackpoint.getCountByWorkout(workout.getId()));
//        workout.setEncodingPoints(Trackpoint.getPolyline(workout.getId(), 100.0d));
//        Log.d(TAG, "segments : " + workout.getSegmentIndex() + " \n points : " + workout.getEncodingPoints());
//        workout.save();

    }

    public static List<Workout> getByKeyword(String keyword, long userId) {
        LinkedList linkedList = new LinkedList();
        db = LitePal.getDatabase();
        Cursor rawQuery = db.rawQuery("SELECT id, sport, starttime, distance, duration, uploadstatus, uuid, serverid, title, synchronise, locsource, calorie, elevationgain, hidden, cadencesource, heartsource, isvalid, credits, matchedsegments, threedworkout, likecount, commentcount, isexport FROM workout where ( userid = ? or userid = 0 ) and (serverid like ? or title like ?) order by starttime DESC", new String[]{String.valueOf(userId), keyword + Separators.PERCENT, Separators.PERCENT + keyword + Separators.PERCENT});
        while (rawQuery.moveToNext()) {
            Workout workout = new Workout();
            workout.id = rawQuery.getLong(0);
            workout.sport = rawQuery.getInt(1);
            workout.startTime = rawQuery.getLong(2);
            workout.distance = rawQuery.getDouble(3);
            workout.duration = rawQuery.getLong(4);
            workout.uploadStatus = rawQuery.getInt(5);
            workout.uuid = rawQuery.getString(6);
            workout.serverId = rawQuery.getLong(7);
            workout.title = rawQuery.getString(8);
            workout.synchronise = rawQuery.getInt(9) == 1;
            workout.locSource = rawQuery.getInt(10);
            workout.calorie = rawQuery.getInt(11);
            workout.elevationGain = rawQuery.getFloat(12);
            workout.hidden = rawQuery.getInt(13);
            workout.cadenceSource = rawQuery.getInt(14);
            workout.heartSource = rawQuery.getInt(15);
            workout.isValid = rawQuery.getInt(16) == 1;
            workout.credits = rawQuery.getInt(17);
            workout.matchedSegments = rawQuery.getInt(18);
            workout.threedWorkout = rawQuery.getString(19);
            workout.likeCount = rawQuery.getInt(20);
            workout.commentCount = rawQuery.getInt(21);
            workout.isExport = rawQuery.getInt(22) == 1;
            linkedList.add(workout);
        }
        rawQuery.close();
        return linkedList;
    }

    public static long getCountByUser(long userId) {
        String userId1 = String.valueOf(userId);

        List<Workout> l = LitePal.where("userId=? or userId = 0", userId1).find(Workout.class);
        long count = l.size();
        return count;
    }

    public static long getUnuploadCountByUser(long userId) {
        String userIdStr = String.valueOf(userId);
        String uploadStatus = String.valueOf((int) ParseUploadStatus(Enums.UploadStatus.NotUpload));//LOCATION_SOURCE_BICI == 2
        List<Workout> ll = LitePal.where(" ( userId = ? or userId = 0 ) and uploadStatus = ? and locSource != 2", userIdStr, uploadStatus).find(Workout.class);
        if (ll != null && !ll.isEmpty()) {
            long size = ll.size();
            return size;
        }

        return 0;

//        return Select.from(Workout.class).where(" ( user_id = ? or user_id = 0 ) and upload_status = ? and loc_source != 2", new String[]{String.valueOf(userId), String.valueOf((int) ParseUploadStatus(Enums.UploadStatus.NotUpload))}).count();

    }

    public static Workout getLastByUser(long userId) {
        String userIdStr = String.valueOf(userId);//LOCATION_SOURCE_PHONE == 1
        Workout w = LitePal.where("(userId=? or userId=0) and locSource=1", userIdStr).order("startTime desc").findFirst(Workout.class);
        return w;
    }

    public static Workout getLastExceptionWorkout(long userId) {
        Workout lastByUser = getLastByUser(userId);
        if (lastByUser == null || lastByUser.getWorkStatus() == Workout.STATUS_STOPED) {//32
            return null;
        }
        return lastByUser;
    }

    public static List<Workout> getUnUploadWorkouts(long uid) {
        //TODO:sun

        String userId = String.valueOf(uid);
        int status = ParseUploadStatus(Enums.UploadStatus.NotUpload);
        String uploadStatus = String.valueOf(status);

        return LitePal.where(" ( userId = ? or userId = 0 ) and uploadStatus = ?", userId, uploadStatus).find(Workout.class);

        //new String[]{String.valueOf(j), String.valueOf((int) ParseUploadStatus(Enums.UploadStatus.NotUpload))}

    }

    public static List<Workout> getTodaysWorkouts(long j) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(calendar.get(1), calendar.get(2), calendar.get(5), 0, 0, 0);

        String userId = String.valueOf(j);
        String startTime = String.valueOf(calendar.getTime().getTime());


        List<Workout> l = LitePal.where("(userId=? or userId=0) and startTime>?", userId, startTime).find(Workout.class);
        return l;

//        return Select.from(Workout.class).where(" ( user_id = ? or user_id = 0 ) and start_time > ?", new String[]{String.valueOf(j), String.valueOf(calendar.getTime().getTime())}).list();
    }

    public static Workout getByUuid(String str) {
		return LitePal.where("uuid=?", str).findFirst(Workout.class);
    }

    public static Workout getById(long j) {

        String id = String.valueOf(j);
        Workout w = LitePal.where("id=?", id).findFirst(Workout.class);
        return w;

//        List find = find(Workout.class, "id = ?", String.valueOf(j));
//        if (find.size() > 0) {
//            return (Workout) find.get(0);
//        }
//        return null;
    }

    public static Workout getByPath(String fitFileName) {
        return LitePal.where("fitPath=?", fitFileName).findFirst(Workout.class);
    }

    public static Workout getByServerId(long serverId) {
        String id = String.valueOf(serverId);
		return LitePal.where("serverId=?", id).findFirst(Workout.class);
    }

    public static WorkoutExtraInfo getWorkoutExtraInfo(long vWorkoutId) {
        WorkoutExtraInfo workoutExtraInfo;
        Cursor rawQuery = LitePal.getDatabase().rawQuery(
                "select MAX(speed) as max_speed, MIN(altitude) as min_altitude," +
                        " MAX(altitude) as max_altitude, AVG(altitude) as avg_altitude," +
                        " MAX(power) as max_power, AVG(power) as avg_power from trackpoint where workoutid = ?",
                new String[]{Long.toString(vWorkoutId)});
        if (rawQuery.moveToFirst()) {
            workoutExtraInfo = new WorkoutExtraInfo();
            int maxSpeedIndex = rawQuery.getColumnIndex("max_speed");
            workoutExtraInfo.setMaxSpeed(rawQuery.getDouble(maxSpeedIndex));
            int maxAltitudeIndex = rawQuery.getColumnIndex("max_altitude");
            workoutExtraInfo.setMaxAltitude(rawQuery.getDouble(maxAltitudeIndex));
            int minAltitudeIndex = rawQuery.getColumnIndex("min_altitude");
            workoutExtraInfo.setMinAltitude(rawQuery.getDouble(minAltitudeIndex));
            int avgAltitudeIndex = rawQuery.getColumnIndex("avg_altitude");
            workoutExtraInfo.setAvgAltitude(rawQuery.getDouble(avgAltitudeIndex));
            int maxPowerIndex = rawQuery.getColumnIndex("max_power");
            workoutExtraInfo.setMaxPower(rawQuery.getDouble(maxPowerIndex));
            int avgPowerIndex = rawQuery.getColumnIndex("avg_power");
            workoutExtraInfo.setAvgPower(rawQuery.getDouble(avgPowerIndex));
        } else {
            workoutExtraInfo = null;
        }
        rawQuery.close();
        return workoutExtraInfo;
    }

    public static short ParseUploadStatus(Enums.UploadStatus uploadStatus) {
        if (uploadStatus == Enums.UploadStatus.NotUpload) {
            return (short) 0;
        }
        if (uploadStatus == Enums.UploadStatus.Uploading) {
            return (short) 1;
        }
        return uploadStatus == Enums.UploadStatus.Uploaded ? (short) 2 : (short) -1;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public boolean canChangeType() {
        return typeChangeCount < 1;
    }

    public Workout clone() {
        try {
            return (Workout) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }

    public ContentValues toContentValues() {
        ContentValues contentValues = new ContentValues();
        if (id != null) {
            contentValues.put("id", id);
        }
        contentValues.put("sport", sport);
        contentValues.put("starttime", startTime);
        contentValues.put("endtime", endTime);
        contentValues.put("distance", distance);
        contentValues.put("duration", duration);
        contentValues.put("userid", userId);
        contentValues.put("uploadstatus", uploadStatus);
        contentValues.put("lastupload", lastUpload);
        contentValues.put("serverid", serverId);
        String str = title;
        if (str != null) {
            contentValues.put("title", str);
        }
        contentValues.put("mingrade", minGrade);
        contentValues.put("maxgrade", maxGrade);
        contentValues.put("elevationgain", elevationGain);
        contentValues.put("elevationloss", elevationLoss);
        contentValues.put("updistance", upDistance);
        contentValues.put("upduration", upDuration);
        contentValues.put("downdistance", downDistance);
        contentValues.put("downduration", downDuration);
        contentValues.put("flatdistance", flatDistance);
        contentValues.put("flatduration", flatDuration);
        String str2 = uuid;
        if (str2 != null) {
            contentValues.put("uuid", str2);
        }
        contentValues.put("hidden", hidden);
        contentValues.put("avgspeed", avgSpeed);
        contentValues.put("maxspeed", maxSpeed);
        contentValues.put("realtimespeed", realtimeSpeed);
        contentValues.put("pauseduration", pauseDuration);
        contentValues.put("avgcadence", avgCadence);
        contentValues.put("maxcadence", maxCadence);
        contentValues.put("avgheartrate", avgHeartrate);
        contentValues.put("maxheartrate", maxHeartrate);
        contentValues.put("startcadence", startCadence);
        contentValues.put("startwheel", startWheel);
        contentValues.put("endcadence", endCadence);
        contentValues.put("endwheel", endWheel);
        contentValues.put("maxwheelrevolution", maxWheelRevolution);
        contentValues.put("locsource", locSource);
        contentValues.put("heartsource", heartSource);
        contentValues.put("cadencesource", cadenceSource);
        String str3 = encodingPoints;
        if (str3 != null) {
            contentValues.put("encodingpoints", str3);
        }
        contentValues.put("pointcounts", pointCounts);
        contentValues.put("matchedsegments", matchedSegments);
        String str4 = segmentIndex;
        if (str4 != null) {
            contentValues.put("segmentindex", str4);
        }
        String str5 = segmentKM;
        if (str5 != null) {
            contentValues.put("segmentkm", str5);
        }
        String str6 = segmentSport;
        if (str6 != null) {
            contentValues.put("segmentsport", str6);
        }
        String str7 = segmentHr;
        if (str7 != null) {
            contentValues.put("segmenthr", str7);
        }
        String str8 = segmentCa;
        if (str8 != null) {
            contentValues.put("segmentca", str8);
        }
        contentValues.put("cadence", cadence);
        contentValues.put("heartrate", heartrate);
        contentValues.put("calorie", calorie);
        contentValues.put("step", step);
        contentValues.put("credits", credits);
        contentValues.put("creditsdvalue", creditsDvalue);
        contentValues.put("avgheatkmrate", avgHeatKmRate);
        contentValues.put("avgheatkm", avgHeatKm);
        contentValues.put("likecount", likeCount);
        contentValues.put("commentcount", commentCount);
        contentValues.put("islike", isLike ? 1 : 0);
        contentValues.put("threedworkout", threedWorkout);
        contentValues.put("isexport", isExport ? 1 : 0);
        contentValues.put("isvalid", isValid ? 1 : 0);
        contentValues.put("poi", poi);
        contentValues.put("synchronise", synchronise ? 1 : 0);
        contentValues.put("mapurl", mapUrl);
        contentValues.put("thumbnail", thumbnail);
        contentValues.put("description", description);
        contentValues.put("fulldata", fullData ? 1 : 0);
        contentValues.put("uploadtime", uploadTime);
        contentValues.put("modifytime", modifyTime);
        contentValues.put("workstatus", workStatus);
        contentValues.put("powersource", powerSource);
        contentValues.put("maxpower", maxPower);
        contentValues.put("avgpower", avgPower);
        contentValues.put("powernp", powerNP);
        contentValues.put("powerif", powerIF);
        contentValues.put("powervi", powerVI);
        contentValues.put("powertss", powerTSS);
        contentValues.put("powerftp", powerFTP);
        contentValues.put("typechangecount", typeChangeCount);
        contentValues.put("category", category);
        contentValues.put("mergerecord", mergeRecord);
        contentValues.put("fitpath", fitPath);
        contentValues.put("equipmentinfo", equipmentInfo);
        contentValues.put("slopegrademax", slopeGradeMax);
        return contentValues;
    }

    @Override
    public void checkPacePoint() {
        if (WorkoutDatabaseHelper.countPacePoint(uuid) > 0 || TextUtils.isEmpty(segmentKM)) {
            return;
        }
        try {
            savePacePoint();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void savePacePoint() throws JSONException {
        if (!TextUtils.isEmpty(segmentKM)) {
            JSONArray jSONArray = new JSONArray(segmentKM);
            ArrayList arrayList = new ArrayList(jSONArray.length());
            for (int i = 0; i < jSONArray.length(); i++) {
                arrayList.add(new PacePoint(jSONArray.getJSONObject(i), this));
            }
            WorkoutDatabaseHelper.savePacePoints(arrayList);
        }
    }

//    @Override
//    public long save() {
//        long save = super.save();
//        Log.i("db", "workout [#" + save + "] saved ! title: " + title);
//        return save;
//    }

    public void flushUploadState() {
        //TODO:sun
        Cursor rawQuery = LitePal.getDatabase().rawQuery("SELECT serverid, uploadstatus, credits FROM workout WHERE id=?", new String[]{String.valueOf(id)});
        if (rawQuery.moveToFirst()) {
            long j = rawQuery.getLong(0);
            Short valueOf = rawQuery.getShort(1);
            double d = rawQuery.getDouble(2);
            setServerId(j);
            setUploadStatus(valueOf);
            setCredits(d);
        }
        rawQuery.close();
    }

    @Override
    public int getCreditsInt() {
        return (int) Math.round(credits);
    }

    public double getCreditsDvalue() {
        return creditsDvalue;
    }

    public void setCreditsDvalue(double creditsDvalue) {
        this.creditsDvalue = creditsDvalue;
    }

    public int getCreditsDvalueInt() {
        return (int) Math.round(creditsDvalue);
    }

    public double getCredits() {
        return credits;
    }

    public void setCredits(double credits) {
        this.credits = credits;
    }

    public String getAvgHeatKmRate() {
        return avgHeatKmRate;
    }

    public void setAvgHeatKmRate(String avgHeatKmRate) {
        this.avgHeatKmRate = avgHeatKmRate;
    }

    public double getAvgHeatKm() {
        return avgHeatKm;
    }

    public void setAvgHeatKm(double avgHeatKm) {
        this.avgHeatKm = avgHeatKm;
    }

    public int getWorkStatus() {
        return workStatus;
    }

    public void setWorkStatus(int workStatus) {
        this.workStatus = workStatus;
    }

    @Override
    public String getDescription() {
        String str = description;
        return str == null ? "" : str;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getHidden() {
        return hidden;
    }

    public void setHidden(int hidden) {
        this.hidden = hidden;
    }

    public String getMapUrl() {
        return mapUrl;
    }

    public void setMapUrl(String mapUrl) {
        this.mapUrl = mapUrl;
    }

    public ServerUser getServerUser() {
        return serverUser;
    }

    public void setServerUser(ServerUser serverUser) {
        this.serverUser = serverUser;
    }

    public void addElevationGain(float elevationGain) {
        this.elevationGain += elevationGain;
    }

    public void addElevationLoss(float elevationLoss) {
        this.elevationLoss += elevationLoss;
    }

    public void addDistance(double distance) {
        this.distance += distance;
    }

    public void addUpDistance(double upDistance) {
        this.upDistance += upDistance;
    }

    public void addUpDuration(long upDuration) {
        this.upDuration += upDuration;
    }

    public void addDownDistance(double downDistance) {
        this.downDistance += downDistance;
    }

    public void addDownDuration(long downDuration) {
        this.downDuration += downDuration;
    }

    public void addFlatDistance(double flatDistance) {
        this.flatDistance += flatDistance;
    }

    public void addFlatDuration(long flatDuration) {
        this.flatDuration += flatDuration;
    }

    public void addPauseDuration(long pauseDuration) {
        this.pauseDuration += pauseDuration;
    }

    public void addDuration(long duration) {
        this.duration += duration;
    }

    @Override
    public Enums.UploadStatus getUploadStatus() {
        Enums.UploadStatus uploadStatus = Enums.UploadStatus.NotUpload;
        int i = this.uploadStatus;
        if (i != 0) {
            if (i == 1) {
                return Enums.UploadStatus.Uploading;
            }
            return i != 2 ? uploadStatus : Enums.UploadStatus.Uploaded;
        }
        return Enums.UploadStatus.NotUpload;
    }

    public void setUploadStatus(Enums.UploadStatus status) {
        this.uploadStatus = ParseUploadStatus(status);
    }

    public void setUploadStatus(short uploadStatus) {
        this.uploadStatus = uploadStatus;
    }

    @Override
    public int getCalorie() {
        Log.d(TAG, "getCalorie calorie=" + calorie);
//        if (calorie < 1000) {
//            calorie = CalorieUtil.calcTotalCalorie(sport, distance);
//        }
        return calorie;
    }

    public void setCalorie(int calorie) {
        this.calorie = calorie;
//        Log.d(TAG, "setCalorie incoming_calorie=" + calorie + ", this.calorie=" + this.calorie);
    }

    public void addCalorie(int calorie) {
        Log.d(TAG, "addCalorie calorie=" + calorie);
        this.calorie += calorie;
    }

    @Override
    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }

    public boolean isSynchronise() {
        return synchronise;
    }

    public void setSynchronise(boolean synchronise) {
        this.synchronise = synchronise;
    }

    @Override
    public int getCadence() {
        return cadence;
    }

    @Override
    public void setCadence(int cadence) {
        this.cadence = cadence;
    }

    @Override
    public int getHeartrate() {
        return heartrate;
    }

    public void setHeartrate(int heartrate) {
        this.heartrate = heartrate;
    }

    @Override
    public int getStartCadence() {
        return startCadence;
    }

    public void setStartCadence(int startCadence) {
        this.startCadence = startCadence;
    }

    @Override
    public int getStartWheel() {
        return startWheel;
    }

    public void setStartWheel(int startWheel) {
        this.startWheel = startWheel;
    }

    @Override
    public int getEndCadence() {
        return endCadence;
    }

    public void setEndCadence(int endCadence) {
        this.endCadence = endCadence;
    }

    @Override
    public int getEndWheel() {
        return endWheel;
    }

    public void setEndWheel(int endWheel) {
        this.endWheel = endWheel;
    }

    @Override
    public int getMaxHeartrate() {
        return maxHeartrate;
    }

    public void setMaxHeartrate(int maxHeartrate) {
        this.maxHeartrate = maxHeartrate;
    }

    @Override
    public int getAvgCadence() {
        return avgCadence;
    }

    public void setAvgCadence(int avgCadence) {
        this.avgCadence = avgCadence;
    }

    @Override
    public int getMaxCadence() {
        return maxCadence;
    }

    public void setMaxCadence(int maxCadence) {
        this.maxCadence = maxCadence;
    }

    @Override
    public int getAvgHeartrate() {
        return avgHeartrate;
    }

    public void setAvgHeartrate(int avgHeartrate) {
        this.avgHeartrate = avgHeartrate;
    }

    @Override
    public double getMaxSpeed() {
        return maxSpeed;
    }

    @Override
    public void setMaxSpeed(double maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public double getRealtimeSpeed() {
        return realtimeSpeed;
    }

    public void setRealtimeSpeed(double realtimeSpeed) {
        this.realtimeSpeed = realtimeSpeed;
    }

    @Override
    public double getAvgSpeed() {
        return avgSpeed;
    }

    public void setAvgSpeed(double avgSpeed) {
        this.avgSpeed = avgSpeed;
    }

    @Override
    public List<ITrackPoint> getByWorkout() {
        List<Trackpoint> queryTrackPoint = WorkoutDatabaseHelper.queryTrackPoint(id);
        ArrayList arrayList = new ArrayList(queryTrackPoint.size());
        arrayList.addAll(queryTrackPoint);
        return arrayList;
    }

    @Override
    public List<ITrackPoint> getByWorkoutForMap() {
        return Trackpoint.getByWorkoutForMap(id);
    }

    @Override
    public List<ITrackPoint> getByWorkoutForWatermark() {
        return Trackpoint.getByWorkoutForWatermark(id);
    }

    @Override
    public List<ITrackPoint> getByWorkoutForMap(int i) {
        return Trackpoint.getByWorkoutForMapSample(id, i, 10000);
    }

    @Override
    public List<ITrackPoint> getByWorkoutForStartEndTimeSample(long j, long j2, int i, int i2) {
        return Trackpoint.getByWorkoutForStartEndTimeSample(id, i, j, j2, i2);
    }

    @Override
    public List<ITrackPoint> getTrackPointsForChart() {
        return Trackpoint.getTrackpointsForChart(id);
    }

    @Override
    public List<ITrackPoint> getTrackPointsForChartAbove0Speed() {
        return Trackpoint.getTrackpointsForChartAbove0Speed(id);
    }

    @Override
    public List<ITrackPoint> getTrackPointsForChartAbove30Heartrate() {
        return Trackpoint.getTrackpointsForChartAbove30Heartrate(id);
    }

    @Override
    public boolean upDatePointPower(List<Float> list, List<Float> list2) {

        String id = String.valueOf(getBaseObjId());
        List<Trackpoint> list3 = LitePal.where("workoutId=?", id).find(Trackpoint.class);

//        List list3 = Select.from(Trackpoint.class).where("workout_id = ?", new String[]{String.valueOf(id)}).list();

        if (list3.size() > 0) {
            for (int i = 0; i < list3.size(); i++) {
                try {
                    ((Trackpoint) list3.get(i)).setPower(list.get(i));
                    ((Trackpoint) list3.get(i)).setDistance(list2.get(i));
                    ((Trackpoint) list3.get(i)).save();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
        return false;
    }

    public List<ITrackPoint> getTrackPointForPower() {
        SQLiteDatabase db2 = LitePal.getDatabase();
        db = db2;
        Cursor rawQuery = db2.rawQuery("SELECT " + Trackpoint.COLUMNS_FOR_POWER + " FROM trackpoint where workoutid = ?", new String[]{String.valueOf(id)});
        int count = rawQuery.getCount();
        if (count < 0) {
            count = 0;
        }
        ArrayList arrayList = new ArrayList(count);
        while (rawQuery.moveToNext()) {
            Trackpoint trackpoint = new Trackpoint();
            trackpoint.setId(rawQuery.getLong(0));
            trackpoint.setTime(rawQuery.getLong(1));
            trackpoint.setPower(rawQuery.getDouble(2));
            arrayList.add(trackpoint);
        }
        rawQuery.close();
        return arrayList;
    }

    public void queryPowerAvgAndMax() {
        SQLiteDatabase db2 = LitePal.getDatabase();
        db = db2;
        Cursor rawQuery = db2.rawQuery("select AVG(power) as avg_power, MAX(power) as max_power from trackpoint where workoutid = ?", new String[]{String.valueOf(id)});
        if (rawQuery.moveToFirst()) {
            int avgIndex = rawQuery.getColumnIndex("avg_power");
            avgPower = rawQuery.getDouble(avgIndex);
            int maxIndex = rawQuery.getColumnIndex("max_power");
            maxPower = rawQuery.getDouble(maxIndex);

        }
        rawQuery.close();

    }

    @Override
    public long getCountByWorkout() {
        return Trackpoint.getCountByWorkout(id);
    }

    @Override
    public long getHeartrateCountByWorkout() {
        return Trackpoint.getHeartrateCountByWorkout(id);
    }

    @Override
    public long getPowerCountByWorkout() {
        return Trackpoint.getPowerCountByWorkout(id);
    }

    @Override
    public long getCadenceCountByWorkout() {
        return Trackpoint.getCadenceCountByWorkout(id);
    }

    @Override
    public long getCountInHeartrateSection(int i, int i2) {
        return Trackpoint.getCountInHeartrateSection(id, i, i2);
    }

    @Override
    public long getCountInPowerSection(int i, int i2) {
        return Trackpoint.getCountInPowerSection(id, i, i2);
    }

    @Override
    public long getCountInCadenceSection(int i, int i2) {
        return Trackpoint.getCountInCadenceSection(id, i, i2);
    }

    @Override
    public long getCadenceSumByWorkout() {
        return Trackpoint.getCadenceSumByWorkout(id);
    }

    @Override
    public WorkoutExtraInfo getWorkoutExtraInfo() {
        return getWorkoutExtraInfo(id);
    }

    @Override
    public int getSport() {
        return sport;
    }

    public void setSport(int sport) {
        this.sport = sport;
    }

    @Override
    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    @Override
    public long getEndTime() {
        return endTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    @Override
    public double getDistance() {
        return distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    @Override
    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    @Override
    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public long getLastUpload() {
        return lastUpload;
    }

    public void setLastUpload(long lastUpload) {
        this.lastUpload = lastUpload;
    }

    @Override
    public long getServerId() {
        return serverId;
    }

    public void setServerId(long serverId) {
        this.serverId = serverId;
    }

    @Override
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public int getMinGrade() {
        return minGrade;
    }

    public void setMinGrade(int minGrade) {
        this.minGrade = minGrade;
    }

    @Override
    public int getMaxGrade() {
        return maxGrade;
    }

    public void setMaxGrade(int maxGrade) {
        this.maxGrade = maxGrade;
    }

    public float getGrade() {
        return grade;
    }

    public void setGrade(float grade) {
        this.grade = grade;
    }

    @Override
    public float getElevationGain() {
        return elevationGain;
    }

    public void setElevationGain(float elevationGain) {
        this.elevationGain = elevationGain;
    }

    @Override
    public float getElevationLoss() {
        return elevationLoss;
    }

    public void setElevationLoss(float elevationLoss) {
        this.elevationLoss = elevationLoss;
    }

    @Override
    public double getUpDistance() {
        return upDistance;
    }

    public void setUpDistance(double upDistance) {
        this.upDistance = upDistance;
    }

    @Override
    public long getUpDuration() {
        return upDuration;
    }

    public void setUpDuration(long upDuration) {
        this.upDuration = upDuration;
    }

    @Override
    public double getDownDistance() {
        return downDistance;
    }

    public void setDownDistance(double downDistance) {
        this.downDistance = downDistance;
    }

    @Override
    public long getDownDuration() {
        return downDuration;
    }

    public void setDownDuration(long downDuration) {
        this.downDuration = downDuration;
    }

    @Override
    public double getFlatDistance() {
        return flatDistance;
    }

    public void setFlatDistance(double flatDistance) {
        this.flatDistance = flatDistance;
    }

    @Override
    public long getFlatDuration() {
        return flatDuration;
    }

    public void setFlatDuration(long flatDuration) {
        this.flatDuration = flatDuration;
    }

    @Override
    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public int getMatchedSegments() {
        return matchedSegments;
    }

    public void setMatchedSegments(int segments) {
        this.matchedSegments = segments;
    }

    @Override
    public boolean hasMatchedSegment() {
        return matchedSegments > 0;
    }

    public long getPauseDuration() {
        return pauseDuration;
    }

    public void setPauseDuration(long pauseDuration) {
        this.pauseDuration = pauseDuration;
    }

    @Override
    public int getMaxWheelRevolution() {
        return maxWheelRevolution;
    }

    public void setMaxWheelRevolution(int maxWheelRevolution) {
        this.maxWheelRevolution = maxWheelRevolution;
    }

    @Override
    public int getLocSource() {
        return locSource;
    }

    public void setLocSource(int locSource) {
        this.locSource = locSource;
    }

    @Override
    public int getHeartSource() {
        return heartSource;
    }

    public void setHeartSource(int heartSource) {
        this.heartSource = heartSource;
    }

    @Override
    public int getCadenceSource() {
        return cadenceSource;
    }

    public void setCadenceSource(int cadenceSource) {
        this.cadenceSource = cadenceSource;
    }

    @Override
    public String getEncodingPoints() {
        return encodingPoints;
    }

    public void setEncodingPoints(String encodingPoints) {
        this.encodingPoints = encodingPoints;
    }

    @Override
    public long getPointCounts() {
        return pointCounts;
    }

    public long computePointCounts() {
        return Trackpoint.getCountByWorkout(id);
    }

    @Override
    public void setPointCounts(long pointCounts) {
        this.pointCounts = pointCounts;
    }

    @Override
    public String getSegmentIndex() {
        return segmentIndex;
    }

    public void setSegmentIndex(String segmentIndex) {
        this.segmentIndex = segmentIndex;
    }

    @Override
    public String getSegmentKM() {
        return segmentKM;
    }

    public void setSegmentKM(String segmentKM) {
        this.segmentKM = segmentKM;
    }

    public String getSegmentKMSample(int i) {
        String makeSampleKMInfoJsonString = PacePoint.makeSampleKMInfoJsonString(uuid, i);
        Log.v(TAG, "getSegmentKMSample, sample = " + i + ", strSegmentKMSample = " + makeSampleKMInfoJsonString);
        return makeSampleKMInfoJsonString == null ? "" : makeSampleKMInfoJsonString;
    }

    @Override
    public String getSegmentSport() {
        return segmentSport;
    }

    public void setSegmentSport(String segmentSport) {
        this.segmentSport = segmentSport;
    }

    @Override
    public String getSegmentHr() {
        return segmentHr;
    }

    @Override
    public void setSegmentHr(String segmentHr) {
        this.segmentHr = segmentHr;
    }

    @Override
    public String getSegmentCa() {
        return segmentCa;
    }

    @Override
    public void setSegmentCa(String segmentCa) {
        this.segmentCa = segmentCa;
    }

    @Override
    public boolean isValid() {
        return isValid;
    }

    public void setIsValid(boolean isValid) {
        this.isValid = isValid;
    }

    @Override
    public String getPoi() {
        return poi;
    }

    public void setPoi(String poi) {
        this.poi = poi;
    }

    @Override
    public boolean isLike() {
        return isLike;
    }

    @Override
    public void setIsLike(boolean like) {
        isLike = like;
    }

    @Override
    public int getLikeCount() {
        return likeCount;
    }

    @Override
    public void setLikeCount(int likeCount) {
        this.likeCount = likeCount;
    }

    @Override
    public int getCommentCount() {
        return commentCount;
    }

    @Override
    public void setCommentCount(int commentCount) {
        this.commentCount = commentCount;
    }

    @Override
    public String getThreedWorkout() {
        return threedWorkout;
    }

    @Override
    public boolean isExport() {
        return isExport;
    }

    public void setIsExport(boolean isExport) {
        this.isExport = isExport;
    }

    public String getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(String thumbnail) {
        this.thumbnail = thumbnail;
    }

    public boolean isFullData() {
        return fullData;
    }

    public void setFullData(boolean fullData) {
        this.fullData = fullData;
    }

    public int getFullDataValue() {
        return fullDataValue;
    }

    public void setFullDataValue(int fullDataValue) {
        this.fullDataValue = fullDataValue;
    }

    public long getUploadTime() {
        return uploadTime;
    }

    public void setUploadTime(long uploadTime) {
        this.uploadTime = uploadTime;
    }

    public long getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(long modifyTime) {
        this.modifyTime = modifyTime;
    }

    @Override
    public boolean isThreedWorkout() {
        return !TextUtils.isEmpty(threedWorkout);
    }

    @Override
    public void setThreedWorkout(String threedWorkout) {
        this.threedWorkout = threedWorkout;
    }

    @Override
    public int getPowerSource() {
        return powerSource;
    }

    @Override
    public void setPowerSource(int powerSource) {
        this.powerSource = powerSource;
    }

    @Override
    public double getMaxPower() {
        return maxPower;
    }

    @Override
    public void setMaxPower(double maxPower) {
        this.maxPower = maxPower;
    }

    @Override
    public double getAvgPower() {
        return avgPower;
    }

    @Override
    public void setAvgPower(double avgPower) {
        this.avgPower = avgPower;
    }

    @Override
    public double getPowerNP() {
        return powerNP;
    }

    @Override
    public void setPowerNP(double powerNP) {
        this.powerNP = powerNP;
    }

    @Override
    public double getPowerIF() {
        return powerIF;
    }

    @Override
    public void setPowerIF(double powerIF) {
        this.powerIF = powerIF;
    }

    @Override
    public double getPowerVI() {
        return powerVI;
    }

    @Override
    public void setPowerVI(double powerVI) {
        this.powerVI = powerVI;
    }

    @Override
    public double getPowerTSS() {
        return powerTSS;
    }

    @Override
    public void setPowerTSS(double powerTSS) {
        this.powerTSS = powerTSS;
    }

    @Override
    public double getPowerFTP() {
        return powerFTP;
    }

    @Override
    public void setPowerFTP(double powerFTP) {
        this.powerFTP = powerFTP;
    }

    public int getTypeChangeCount() {
        return typeChangeCount;
    }

    public void setTypeChangeCount(int typeChangeCount) {
        this.typeChangeCount = typeChangeCount;
    }

    @Override
    public int getCategory() {
        return category;
    }

    public void setCategory(int category) {
        this.category = category;
    }

    @Override
    public String getMergeRecord() {
        return mergeRecord;
    }

    public void setMergeRecord(String mergeRecord) {
        this.mergeRecord = mergeRecord;
    }

    public String getFitPath() {
        return fitPath;
    }

    public void setFitPath(String fitPath) {
        this.fitPath = fitPath;
    }

    public String getEquipmentInfo() {
        return equipmentInfo;
    }

    public void setEquipmentInfo(String equipmentInfo) {
        this.equipmentInfo = equipmentInfo;
    }

    public List<TrackSegment> getTrackSegments() {
        return trackSegments;
    }

    public void setTrackSegments(List<TrackSegment> trackSegments) {
        this.trackSegments = trackSegments;
    }

    public List<Trackpoint> getTrackPoints() {
        return trackPoints;
    }

    public void setTrackPoints(List<Trackpoint> trackPoints) {
        this.trackPoints = trackPoints;
    }

    public List<PacePoint> getPacePoints() {
        return pacePoints;
    }

    public void setPacePoints(List<PacePoint> pacePoints) {
        this.pacePoints = pacePoints;
    }

    @Override
    public int getMapHidden() {
        return mapHidden;
    }

    public void setMapHidden(int mapHidden) {
        this.mapHidden = mapHidden;
    }

    @Override
    public int getMapId() {
        return mapId;
    }

    @Override
    public void setMapId(int mapId) {
        this.mapId = mapId;
    }

    @Override
    public String getSlopeGradeMax() {
        return slopeGradeMax;
    }

    public void setSlopeGradeMax(String slopeGradeMax) {
        this.slopeGradeMax = slopeGradeMax;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Workout workout = (Workout) obj;
        if (sport != workout.sport || startTime != workout.startTime || endTime != workout.endTime || Double.compare(workout.distance, distance) != 0 || duration != workout.duration || userId != workout.userId || uploadStatus != workout.uploadStatus || lastUpload != workout.lastUpload || serverId != workout.serverId || minGrade != workout.minGrade || maxGrade != workout.maxGrade || Float.compare(workout.elevationGain, elevationGain) != 0 || Float.compare(workout.elevationLoss, elevationLoss) != 0 || Double.compare(workout.upDistance, upDistance) != 0 || upDuration != workout.upDuration || Double.compare(workout.downDistance, downDistance) != 0 || downDuration != workout.downDuration || Double.compare(workout.flatDistance, flatDistance) != 0 || flatDuration != workout.flatDuration || hidden != workout.hidden || Double.compare(workout.avgSpeed, avgSpeed) != 0 || Double.compare(workout.maxSpeed, maxSpeed) != 0 || Double.compare(workout.realtimeSpeed, realtimeSpeed) != 0 || pauseDuration != workout.pauseDuration || avgCadence != workout.avgCadence || maxCadence != workout.maxCadence || avgHeartrate != workout.avgHeartrate || maxHeartrate != workout.maxHeartrate || startCadence != workout.startCadence || startWheel != workout.startWheel || endCadence != workout.endCadence || endWheel != workout.endWheel || maxWheelRevolution != workout.maxWheelRevolution || locSource != workout.locSource || heartSource != workout.heartSource || cadenceSource != workout.cadenceSource || pointCounts != workout.pointCounts || matchedSegments != workout.matchedSegments || cadence != workout.cadence || heartrate != workout.heartrate || calorie != workout.calorie || step != workout.step || Double.compare(workout.credits, credits) != 0 || Double.compare(workout.creditsDvalue, creditsDvalue) != 0 || Double.compare(workout.avgHeatKm, avgHeatKm) != 0) {
            return false;
        }
        String str = avgHeatKmRate;
        if (str == null ? workout.avgHeatKmRate != null : !str.equals(workout.avgHeatKmRate)) {
            return false;
        }
        if (likeCount != workout.likeCount || commentCount != workout.commentCount || isLike != workout.isLike || isExport != workout.isExport || isValid != workout.isValid || synchronise != workout.synchronise || fullData != workout.fullData || uploadTime != workout.uploadTime || modifyTime != workout.modifyTime || workStatus != workout.workStatus || powerSource != workout.powerSource || Double.compare(workout.maxPower, maxPower) != 0 || Double.compare(workout.avgPower, avgPower) != 0 || Double.compare(workout.powerNP, powerNP) != 0 || Double.compare(workout.powerIF, powerIF) != 0 || Double.compare(workout.powerVI, powerVI) != 0 || Double.compare(workout.powerTSS, powerTSS) != 0 || Double.compare(workout.powerFTP, powerFTP) != 0 || typeChangeCount != workout.typeChangeCount || category != workout.category || Float.compare(workout.grade, grade) != 0) {
            return false;
        }
        String str2 = title;
        if (str2 == null ? workout.title != null : !str2.equals(workout.title)) {
            return false;
        }
        String str3 = uuid;
        if (str3 == null ? workout.uuid != null : !str3.equals(workout.uuid)) {
            return false;
        }
        String str4 = encodingPoints;
        if (str4 == null ? workout.encodingPoints != null : !str4.equals(workout.encodingPoints)) {
            return false;
        }
        String str5 = segmentIndex;
        if (str5 == null ? workout.segmentIndex != null : !str5.equals(workout.segmentIndex)) {
            return false;
        }
        String str6 = segmentKM;
        if (str6 == null ? workout.segmentKM != null : !str6.equals(workout.segmentKM)) {
            return false;
        }
        String str7 = segmentSport;
        if (str7 == null ? workout.segmentSport != null : !str7.equals(workout.segmentSport)) {
            return false;
        }
        String str8 = segmentHr;
        if (str8 == null ? workout.segmentHr != null : !str8.equals(workout.segmentHr)) {
            return false;
        }
        String str9 = segmentCa;
        if (str9 == null ? workout.segmentCa != null : !str9.equals(workout.segmentCa)) {
            return false;
        }
        String str10 = threedWorkout;
        if (str10 == null ? workout.threedWorkout != null : !str10.equals(workout.threedWorkout)) {
            return false;
        }
        String str11 = poi;
        if (str11 == null ? workout.poi != null : !str11.equals(workout.poi)) {
            return false;
        }
//        ServerUser serverUser = serverUser;
        if (serverUser == null ? workout.serverUser != null : !serverUser.equals(workout.serverUser)) {
            return false;
        }
        String str12 = mapUrl;
        if (str12 == null ? workout.mapUrl != null : !str12.equals(workout.mapUrl)) {
            return false;
        }
        String str13 = thumbnail;
        if (str13 == null ? workout.thumbnail != null : !str13.equals(workout.thumbnail)) {
            return false;
        }
        String str14 = description;
        if (str14 == null ? workout.description != null : !str14.equals(workout.description)) {
            return false;
        }
        String str15 = mergeRecord;
        if (str15 == null ? workout.mergeRecord != null : !str15.equals(workout.mergeRecord)) {
            return false;
        }
        String str16 = fitPath;
        if (str16 == null ? workout.fitPath != null : !str16.equals(workout.fitPath)) {
            return false;
        }
        String str17 = equipmentInfo;
        if (str17 == null ? workout.equipmentInfo != null : !str17.equals(workout.equipmentInfo)) {
            return false;
        }
        List<TrackSegment> list = trackSegments;
        if (list == null ? workout.trackSegments != null : !list.equals(workout.trackSegments)) {
            return false;
        }
        String str18 = slopeGradeMax;
        if (str18 == null ? workout.slopeGradeMax != null : !str18.equals(workout.slopeGradeMax)) {
            return false;
        }
        List<Trackpoint> list2 = trackPoints;
        List<Trackpoint> list3 = workout.trackPoints;
        return list2 != null ? list2.equals(list3) : list3 == null;
    }

    public int hashCode() {
        long j = startTime;
        long j2 = endTime;
        long doubleToLongBits = Double.doubleToLongBits(distance);
        long j3 = duration;
        long j4 = userId;
        long j5 = lastUpload;
        long j6 = serverId;
        int i = ((((((((((((((((sport * 31) + ((int) (j ^ (j >>> 32)))) * 31) + ((int) (j2 ^ (j2 >>> 32)))) * 31) + ((int) (doubleToLongBits ^ (doubleToLongBits >>> 32)))) * 31) + ((int) (j3 ^ (j3 >>> 32)))) * 31) + ((int) (j4 ^ (j4 >>> 32)))) * 31) + uploadStatus) * 31) + ((int) (j5 ^ (j5 >>> 32)))) * 31) + ((int) (j6 ^ (j6 >>> 32)))) * 31;
        String str = title;
        int i2 = 0;
        int hashCode = (((((i + (str != null ? str.hashCode() : 0)) * 31) + minGrade) * 31) + maxGrade) * 31;
        float f = elevationGain;
        int floatToIntBits = (hashCode + (f != 0.0f ? Float.floatToIntBits(f) : 0)) * 31;
        float f2 = elevationLoss;
        int floatToIntBits2 = f2 != 0.0f ? Float.floatToIntBits(f2) : 0;
        long doubleToLongBits2 = Double.doubleToLongBits(upDistance);
        long j7 = upDuration;
        long doubleToLongBits3 = Double.doubleToLongBits(downDistance);
        long j8 = downDuration;
        long doubleToLongBits4 = Double.doubleToLongBits(flatDistance);
        long j9 = flatDuration;
        int i3 = (((((((((((((floatToIntBits + floatToIntBits2) * 31) + ((int) (doubleToLongBits2 ^ (doubleToLongBits2 >>> 32)))) * 31) + ((int) (j7 ^ (j7 >>> 32)))) * 31) + ((int) (doubleToLongBits3 ^ (doubleToLongBits3 >>> 32)))) * 31) + ((int) (j8 ^ (j8 >>> 32)))) * 31) + ((int) (doubleToLongBits4 ^ (doubleToLongBits4 >>> 32)))) * 31) + ((int) (j9 ^ (j9 >>> 32)))) * 31;
        String str2 = uuid;
        int hashCode2 = str2 != null ? str2.hashCode() : 0;
        long doubleToLongBits5 = Double.doubleToLongBits(avgSpeed);
        long doubleToLongBits6 = Double.doubleToLongBits(maxSpeed);
        long doubleToLongBits7 = Double.doubleToLongBits(realtimeSpeed);
        long j10 = pauseDuration;
        int i4 = (((((((((((((((((((((((((((((((((((i3 + hashCode2) * 31) + hidden) * 31) + ((int) (doubleToLongBits5 ^ (doubleToLongBits5 >>> 32)))) * 31) + ((int) (doubleToLongBits6 ^ (doubleToLongBits6 >>> 32)))) * 31) + ((int) (doubleToLongBits7 ^ (doubleToLongBits7 >>> 32)))) * 31) + ((int) (j10 ^ (j10 >>> 32)))) * 31) + avgCadence) * 31) + maxCadence) * 31) + avgHeartrate) * 31) + maxHeartrate) * 31) + startCadence) * 31) + startWheel) * 31) + endCadence) * 31) + endWheel) * 31) + maxWheelRevolution) * 31) + locSource) * 31) + heartSource) * 31) + cadenceSource) * 31;
        String str3 = encodingPoints;
        int hashCode3 = str3 != null ? str3.hashCode() : 0;
        long j11 = pointCounts;
        int i5 = (((((i4 + hashCode3) * 31) + ((int) (j11 ^ (j11 >>> 32)))) * 31) + matchedSegments) * 31;
        String str4 = segmentIndex;
        int hashCode4 = (i5 + (str4 != null ? str4.hashCode() : 0)) * 31;
        String str5 = segmentKM;
        int hashCode5 = (hashCode4 + (str5 != null ? str5.hashCode() : 0)) * 31;
        String str6 = segmentSport;
        int hashCode6 = (hashCode5 + (str6 != null ? str6.hashCode() : 0)) * 31;
        String str7 = segmentHr;
        int hashCode7 = (hashCode6 + (str7 != null ? str7.hashCode() : 0)) * 31;
        String str8 = segmentCa;
        int hashCode8 = str8 != null ? str8.hashCode() : 0;
        long doubleToLongBits8 = Double.doubleToLongBits(credits);
        long doubleToLongBits9 = Double.doubleToLongBits(creditsDvalue);
        long doubleToLongBits10 = Double.doubleToLongBits(avgHeatKm);
        int i6 = (((((((((((((((hashCode7 + hashCode8) * 31) + cadence) * 31) + heartrate) * 31) + calorie) * 31) + step) * 31) + ((int) (doubleToLongBits8 ^ (doubleToLongBits8 >>> 32)))) * 31) + ((int) (doubleToLongBits9 ^ (doubleToLongBits9 >>> 32)))) * 31) + ((int) (doubleToLongBits10 ^ (doubleToLongBits10 >>> 32)))) * 31;
        String str9 = avgHeatKmRate;
        int hashCode9 = (((((((i6 + (str9 != null ? str9.hashCode() : 0)) * 31) + likeCount) * 31) + commentCount) * 31) + (isLike ? 1 : 0)) * 31;
        String str10 = threedWorkout;
        int hashCode10 = (((((hashCode9 + (str10 != null ? str10.hashCode() : 0)) * 31) + (isExport ? 1 : 0)) * 31) + (isValid ? 1 : 0)) * 31;
        String str11 = poi;
        int hashCode11 = (((hashCode10 + (str11 != null ? str11.hashCode() : 0)) * 31) + (synchronise ? 1 : 0)) * 31;
//        ServerUser serverUser = serverUser;
        int hashCode12 = (hashCode11 + (serverUser != null ? serverUser.hashCode() : 0)) * 31;
        String str12 = mapUrl;
        int hashCode13 = (hashCode12 + (str12 != null ? str12.hashCode() : 0)) * 31;
        String str13 = thumbnail;
        int hashCode14 = (hashCode13 + (str13 != null ? str13.hashCode() : 0)) * 31;
        String str14 = description;
        int hashCode15 = str14 != null ? str14.hashCode() : 0;
        long j12 = uploadTime;
        long j13 = modifyTime;
        long doubleToLongBits11 = Double.doubleToLongBits(maxPower);
        long doubleToLongBits12 = Double.doubleToLongBits(avgPower);
        long doubleToLongBits13 = Double.doubleToLongBits(powerNP);
        long doubleToLongBits14 = Double.doubleToLongBits(powerIF);
        long doubleToLongBits15 = Double.doubleToLongBits(powerVI);
        long doubleToLongBits16 = Double.doubleToLongBits(powerTSS);
        long doubleToLongBits17 = Double.doubleToLongBits(powerFTP);
        int i7 = (((((((((((((((((((((((((((((hashCode14 + hashCode15) * 31) + (fullData ? 1 : 0)) * 31) + ((int) (j12 ^ (j12 >>> 32)))) * 31) + ((int) (j13 ^ (j13 >>> 32)))) * 31) + workStatus) * 31) + powerSource) * 31) + ((int) (doubleToLongBits11 ^ (doubleToLongBits11 >>> 32)))) * 31) + ((int) (doubleToLongBits12 ^ (doubleToLongBits12 >>> 32)))) * 31) + ((int) (doubleToLongBits13 ^ (doubleToLongBits13 >>> 32)))) * 31) + ((int) (doubleToLongBits14 ^ (doubleToLongBits14 >>> 32)))) * 31) + ((int) (doubleToLongBits15 ^ (doubleToLongBits15 >>> 32)))) * 31) + ((int) (doubleToLongBits16 ^ (doubleToLongBits16 >>> 32)))) * 31) + ((int) (doubleToLongBits17 ^ (doubleToLongBits17 >>> 32)))) * 31) + typeChangeCount) * 31) + category) * 31;
        String str15 = mergeRecord;
        int hashCode16 = (i7 + (str15 != null ? str15.hashCode() : 0)) * 31;
        String str16 = fitPath;
        int hashCode17 = (hashCode16 + (str16 != null ? str16.hashCode() : 0)) * 31;
        String str17 = equipmentInfo;
        int hashCode18 = (hashCode17 + (str17 != null ? str17.hashCode() : 0)) * 31;
        List<TrackSegment> list = trackSegments;
        int hashCode19 = (hashCode18 + (list != null ? list.hashCode() : 0)) * 31;
        float f3 = grade;
        int floatToIntBits3 = (hashCode19 + (f3 != 0.0f ? Float.floatToIntBits(f3) : 0)) * 31;
        List<Trackpoint> list2 = trackPoints;
        int hashCode20 = (floatToIntBits3 + (list2 != null ? list2.hashCode() : 0)) * 31;
        String str18 = slopeGradeMax;
        if (str18 != null) {
            i2 = str18.hashCode();
        }
        return hashCode20 + i2;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeLong(id == null ? -1L : id);
        parcel.writeInt(sport);
        parcel.writeLong(startTime);
        parcel.writeLong(endTime);
        parcel.writeDouble(distance);
        parcel.writeLong(duration);
        parcel.writeLong(userId);
        parcel.writeInt(uploadStatus);
        parcel.writeLong(lastUpload);
        parcel.writeLong(serverId);
        parcel.writeString(title);
        parcel.writeInt(minGrade);
        parcel.writeInt(maxGrade);
        parcel.writeFloat(elevationGain);
        parcel.writeFloat(elevationLoss);
        parcel.writeDouble(upDistance);
        parcel.writeLong(upDuration);
        parcel.writeDouble(downDistance);
        parcel.writeLong(downDuration);
        parcel.writeDouble(flatDistance);
        parcel.writeLong(flatDuration);
        parcel.writeString(uuid);
        parcel.writeInt(hidden);
        parcel.writeDouble(avgSpeed);
        parcel.writeDouble(maxSpeed);
        parcel.writeDouble(realtimeSpeed);
        parcel.writeLong(pauseDuration);
        parcel.writeInt(avgCadence);
        parcel.writeInt(maxCadence);
        parcel.writeInt(avgHeartrate);
        parcel.writeInt(maxHeartrate);
        parcel.writeInt(startCadence);
        parcel.writeInt(startWheel);
        parcel.writeInt(endCadence);
        parcel.writeInt(endWheel);
        parcel.writeInt(maxWheelRevolution);
        parcel.writeInt(locSource);
        parcel.writeInt(heartSource);
        parcel.writeInt(cadenceSource);
        parcel.writeString(encodingPoints);
        parcel.writeLong(pointCounts);
        parcel.writeInt(matchedSegments);
        parcel.writeString(segmentIndex);
        parcel.writeString(segmentSport);
        parcel.writeString(segmentHr);
        parcel.writeString(segmentCa);
        parcel.writeInt(cadence);
        parcel.writeInt(heartrate);
        parcel.writeInt(calorie);
        parcel.writeInt(step);
        parcel.writeDouble(credits);
        parcel.writeDouble(creditsDvalue);
        parcel.writeDouble(avgHeatKm);
        parcel.writeString(avgHeatKmRate);
        parcel.writeInt(likeCount);
        parcel.writeInt(commentCount);
        parcel.writeByte(isLike ? (byte) 1 : (byte) 0);
        parcel.writeString(threedWorkout);
        parcel.writeByte(isExport ? (byte) 1 : (byte) 0);
        parcel.writeByte(isValid ? (byte) 1 : (byte) 0);
        parcel.writeString(poi);
        parcel.writeByte(synchronise ? (byte) 1 : (byte) 0);
        parcel.writeParcelable(serverUser, i);
        parcel.writeString(mapUrl);
        parcel.writeString(thumbnail);
        parcel.writeString(description);
        parcel.writeByte(fullData ? (byte) 1 : (byte) 0);
        parcel.writeLong(uploadTime);
        parcel.writeLong(modifyTime);
        parcel.writeInt(workStatus);
        parcel.writeInt(powerSource);
        parcel.writeDouble(maxPower);
        parcel.writeDouble(avgPower);
        parcel.writeDouble(powerNP);
        parcel.writeDouble(powerIF);
        parcel.writeDouble(powerVI);
        parcel.writeDouble(powerTSS);
        parcel.writeDouble(powerFTP);
        parcel.writeInt(typeChangeCount);
        parcel.writeInt(category);
        parcel.writeString(mergeRecord);
        parcel.writeString(fitPath);
        parcel.writeString(equipmentInfo);
        parcel.writeFloat(grade);
        parcel.writeInt(mapHidden);
        parcel.writeInt(mapId);
        parcel.writeString(slopeGradeMax);
    }
}